<template>
	<view>
		<u-cell-group>
				<u-cell-item 
				:arrow-direction="show?'down':'right'"
				@click="show = true" 
				icon="grid" 
				style="position: fixed; top: 0px;  background: white; z-index: 10;"
				:title="tableInfo.label?tableInfo.label:'选着评价表'"></u-cell-item>
		</u-cell-group>
		<u-select  v-model="show" :list="evaluateList" @confirm="confirm"></u-select>
		<u-empty v-if="!tableInfo.label" style="position: relative;top: 300rpx;" text="暂无数据" mode="list"></u-empty>
		<br/><br/><br/>
		<u-card :head-style="{'padding':'8px'}" 
				v-for="(item,index) in evaluateInfo.questionsDOList" 
				:key="item.eqId" 
				:title="item.name!=undefined?item.name:item.commentcontent" >
			<view  slot="body">
				<view v-if="item.child==undefined">
					<view>
						<u-input v-model="item.userInputText"  type="textarea"></u-input>
					</view>
				</view>
				<view style="line-height: 45rpx;" v-else>
					<view  v-for="(item2,index) in item.child" :key="index">
						<p>{{(index+1)+'、'+item2.commentcontent}}</p>
						<u-radio-group  v-if="!item2.scoring" style="position: relative; left: 40rpx;" v-model="item2.radioValue">
							<u-radio  v-for="item3 in item2.scoreitemDOList" :name="item3.fraction" :key="item2.sid">{{item3.content}}</u-radio>
						</u-radio-group>
						<view  v-else>
							<u-row>
								<u-col span="11">
									<slider :max="Math.max.apply(Math,item2.scoreitemDOList.map(m=>m.fraction))" 
									@changing="(e)=>{item2.radioValue = e.detail.value}">
									</slider>
								</u-col>
								<u-col span="1">
									{{item2.radioValue}}
								</u-col>
							</u-row>
						</view>
					</view>
				</view> 
			</view>
		</u-card>
		<u-card :head-style="{'padding':'8px'}"  v-if="tableInfo.label" title="总体评价">
			<view  slot="body">
				<u-row  gutter="16" justify="center">
					<u-radio-group v-model="evaluateInfo.overallScore" >
						<u-radio  v-for="item in overallScoreList" 
						:key="item.value"  
						:name="item.value">
						{{item.name}}
						</u-radio>
					</u-radio-group>
				</u-row>
			</view>
		</u-card>
		<u-button style="margin: 30rpx" v-if="tableInfo.label" @click="modelshow = true" type="success">保存</u-button>
		<u-modal v-model="modelshow" show-cancel-button cancel-text="取消" @confirm="saveUserEvaluate">
			<br />
				<u-row justify="center" gutter="16">
					是否要保存评价?
				</u-row>
			<br />
		</u-modal>
		<br/><br/>
		<u-toast ref="uToast" />
		<drag-button :isDock="true" ></drag-button>
	</view>
</template>

<script>
	import dragButton from '../../components/drag-button/drag-button.vue'
	export default{
		components:{
			dragButton
		},
		data(){
			return{
				modelshow:false,
				show:false,
				classId:null,
				taskTeacherId:null, 
				evaluateList:[],
				evaluateInfo:{},
				tableInfo:{},
				overallScoreList:[
					{
						"value":"D",
						"name":"不合格"
					},
					{
						"value":"C", 
						"name":"合格"
					},
					{
						"value":"B",
						"name":"良好"
					},
					{
						"value":"A",
						"name":"优秀"
					}]
			}
		},
		methods:{
			/**
			 * 获取全部的评价表
			 */
			getAllEvaluate(){
				this.$u.http.get(`/task/getAllEvaluate`).then(res=>{
					if(res.data){
						this.evaluateList = JSON.parse(JSON.stringify(res.data).replace(/evaluateName/g,"label").replace(/eid/g,"value")).map(m=>{
							m.overallScore = 0
							let textlist = m.questionsDOList?.filter(f=>f.evaluationitem == null).map(h=>{
								h.userInputText = null;
								return h;
							})
							let array =Array.from(new Set(m.questionsDOList?.map(m1=> m1.evaluationitem)));
							m.questionsDOList = array.filter(f1=>f1!=null).map(d=>{
								let obj = {"name":d,"child":[]}
								m.questionsDOList?.forEach(f=>{
									if(f.evaluationitem == d){
										f.radioValue=null
										obj.child.push(f)
									}
								})
								return obj;
							})
							m.questionsDOList = m.questionsDOList.concat(textlist)
							return m
						})
					}
				})
			},
			/**
			 * 点击确定下拉框
			 * @param {Object} 
			 */
			confirm(e){
				this.tableInfo = e[0]
				this.evaluateInfo = this.evaluateList.find(f=>f.value==e[0].value)
			},
			/**
			 * 保存评价内容
			 */
			saveUserEvaluate(){
				 //计算选中的总分
				 let userFraction = this.evaluateList.filter(f=>f.value==this.tableInfo.value).map(m=>{
						return eval(m.questionsDOList?.map(e=>{
							return eval(e.child?.filter(s=>s.type === "选项" && s.radioValue).map(w=>{
								return w.radioValue?w.radioValue:0;
							}).join("+"))
						}).filter(h=>h!=undefined).join("+"))
					})
				//获取总分	
				let sumFraction = this.evaluateList.filter(f=>f.value==this.tableInfo.value).map(m=>{
									return eval(m.questionsDOList?.map(e=>{
										return eval(e.child?.filter(s=>s.type === "选项").map(w=>{
											return Math.max.apply(Math,w.scoreitemDOList?.map(m=>m.fraction));
										}).join("+"))
									}).filter(h=>h!=undefined).join("+"))
								})
				//获取输入文本值
				let textSupervisor = this.evaluateList.filter(f=>f.value==this.tableInfo.value).map(m=>{
					return m.questionsDOList?.filter(f1=>f1.child == undefined).map(e=>{
							return  {"title":e.commentcontent,"text":e.userInputText};
						})
					})
					let data = {
							"actualScore":Math.round((userFraction/sumFraction)*100),
							"textSupervisor":textSupervisor[0],
							"overallScore":this.evaluateInfo.overallScore,
							"userId":this.user.userId,
							"taskCourseId":this.classId,
							"evaluateInfo":this.evaluateInfo
							}
			 	this.$u.http.post("/task/saveUserEvaluate",this.qs.parse(data)).then(res=>{
						if(res.data){
							this.$u.toast('成功评价');		
							this.myTaskList.forEach(m=>{
								if(m.taskTeacherId == this.taskTeacherId){
									m.taskDO.classDOList.forEach(g=>{
										if(g.classId == this.classId){
											g.userEvaluateDO = data
										 }
									})
								}
							})		
							this.$store.commit("updateMyTaskList",this.myTaskList)
							this.myTaskList.forEach(m=>{
								if(m.taskTeacherId == this.taskTeacherId){
									let flag = m.taskDO.classDOList?.some(s=> s.userEvaluateDO == null)
									if(!flag){
										this.$u.http.post('/task/updateTaskTeacherStatus',
										this.qs.stringify({"taskTeacherId":this.taskTeacherId})).then(res=>{
											if(res.data){
												m.isComplete = !flag
												this.$store.commit("updateMyTaskList",this.myTaskList)
											}
										})
									}
								}
							})	
							uni.navigateBack(1)
						}
				}) 
			}
		},
		computed:{
			user(){
				return this.$store.state.user
			},
			myTaskList(){
				return this.$store.state.myTaskList
			}
		},
		onLoad(item) {
			this.classId = item.classId
			this.taskTeacherId = item.taskTeacherId
		},
		created() {
			this.getAllEvaluate()
		}
	}
</script>

<style scoped lang="scss">
	page{
			 background-color: #f7f7f7;;
		 }
</style>
